﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using Engine;
using XmlUtilities;

namespace Game
{
    public class Mod : ModLoader
    {
        public List<Point3> BlocksPoint = new List<Point3>();

        public bool NetPlayBool;

        public Widget_JumpUI Widget;

        public Widget_StealthUI Widget2;

        public Subtexture a;

        public Subtexture b;

        public static Vector2 Widget_po;

        public static Vector2 Widget_2po;

        public float dt;

        public override void __ModInitialize()
        {
            base.__ModInitialize();
            ModsManager.RegisterHook("BlocksInitalized", this);
            ModsManager.RegisterHook("SubsystemUpdate", this);
            ModsManager.RegisterHook("OnLevelUpdate", this);
            ModsManager.RegisterHook("UpdateInput", this);
            Window.Frame += OnFrame;
            Subtexture subtexture = new Subtexture(XT.GetTexture2D("3.png"), Vector2.Zero, Vector2.One);
            Widget = new Widget_JumpUI
            {
                NormalSubtexture = subtexture,
                ClickedSubtexture = subtexture,
                Size = new Vector2(100f, 100f)
            };
            subtexture = (a = new Subtexture(XT.GetTexture2D("1.png"), Vector2.Zero, Vector2.One));
            b = new Subtexture(XT.GetTexture2D("2.png"), Vector2.Zero, Vector2.One);
            Widget2 = new Widget_StealthUI
            {
                NormalSubtexture = subtexture,
                ClickedSubtexture = subtexture,
                Size = new Vector2(100f, 100f)
            };
            Widget_po = new Vector2(1800f, 800f);
            Widget_2po = new Vector2(1800f, 600f);
        }

        public override void BlocksInitalized()
        {
            SettingsManager.m_brightness = 1000f;
        }

        public override void SubsystemUpdate(float dt)
        {
            try
            {
                this.dt = dt;
                base.SubsystemUpdate(dt);
                ComponentPlayer mainPlayer = GameManager.Project.FindSubsystem<SubsystemPlayers>().MainPlayer;
                GameWidget gameWidget = mainPlayer.GameWidget;
                gameWidget.AddChildren(Widget);
                gameWidget.AddChildren(Widget2);
                float num = 0.5f / SettingsManager.UIScale;
                Widget.Size = new Vector2(150f * num);
                gameWidget.SetWidgetPosition(Widget, new Vector2(Widget_po.X * num, Widget_po.Y * num));
                Widget2.Size = new Vector2(100f * num);
                gameWidget.SetWidgetPosition(Widget2, new Vector2(Widget_2po.X * num, Widget_2po.Y * num));
                if (mainPlayer.ComponentInput.m_playerInput.Aim.HasValue)
                {
                    mainPlayer.ComponentLocomotion.LookOrder = mainPlayer.ComponentInput.m_playerInput.Look * (SettingsManager.FlipVerticalAxis ? new Vector2(0f, -1f) : new Vector2(0f, 1f));
                    Quaternion rotation = mainPlayer.ComponentBody.Rotation;
                    float num2 = MathUtils.Atan2(2f * rotation.Y * rotation.W - 2f * rotation.X * rotation.Z, 1f - 2f * rotation.Y * rotation.Y - 2f * rotation.Z * rotation.Z);
                    num2 += (0f - mainPlayer.ComponentLocomotion.TurnSpeed) * mainPlayer.ComponentInput.m_playerInput.Look.X * dt;
                    mainPlayer.ComponentBody.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, num2);
                    mainPlayer.m_isDigBlocked = true;
                }
            }
            catch
            {
            }
        }

        public void OnFrame()
        {
            if (!ScreensManager.m_screens.ContainsKey("NetPlay") || NetPlayBool)
            {
                return;
            }

            ReadDirResouces(ModsManager.ModsPath, "");
            ModsManager.ModList.Remove(Entity);
            (ModsManager.ModList[1] as FastDebugModEntity).ResourcesMd5 = string.Empty;
            foreach (KeyValuePair<string, FileInfo> fModFile in (ModsManager.ModList[1] as FastDebugModEntity).FModFiles)
            {
                string key = fModFile.Key;
                (ModsManager.ModList[1] as FastDebugModEntity).GetFile(key, delegate (Stream stream)
                {
                    byte[] array = new byte[stream.Length];
                    stream.Read(array, 0, array.Length);
                    (ModsManager.ModList[1] as FastDebugModEntity).ResourcesMd5 += ModsManager.GetMd5(array);
                });
            }

            NetPlayBool = true;
        }

        public void ReadDirResouces(string basepath, string path)
        {
            (ModsManager.ModList[1] as FastDebugModEntity).FModFiles.Clear();
            if (string.IsNullOrEmpty(path))
            {
                path = basepath;
            }

            foreach (string item in Storage.ListDirectoryNames(path))
            {
                ReadDirResouces(basepath, path + "/" + item);
            }

            foreach (string item2 in Storage.ListFileNames(path))
            {
                string text = path + "/" + item2;
                string key = text.Substring(basepath.Length + 1);
                if (Storage.GetFileName(text) != Storage.GetFileName(Entity.ModFilePath))
                {
                    (ModsManager.ModList[1] as FastDebugModEntity).FModFiles.Add(key, new FileInfo(Storage.GetSystemPath(text)));
                }
            }
        }

        public override void UpdateInput(ComponentInput componentInput, WidgetInput widgetInput)
        {
            try
            {
                if (Widget.Jump)
                {
                    Widget.Jump = false;
                    componentInput.m_playerInput.Jump = true;
                    TouchInput value = componentInput.m_componentGui.MoveWidget.TouchInput.Value;
                    value.InputType = TouchInputType.Tap;
                    Log.Information(value.ToString());
                }

                if (Widget2.Stealth)
                {
                    if (componentInput.m_componentPlayer.ComponentBody.TargetCrouchFactor == 1f)
                    {
                        componentInput.m_componentPlayer.ComponentBody.TargetCrouchFactor = 0f;
                        componentInput.m_componentGui.DisplaySmallMessage(LanguageControl.Get(ComponentGui.fName, 3), Color.White, blinking: false, playNotificationSound: false);
                    }
                    else
                    {
                        componentInput.m_componentPlayer.ComponentBody.TargetCrouchFactor = 1f;
                        componentInput.m_componentGui.DisplaySmallMessage(LanguageControl.Get(ComponentGui.fName, 4), Color.White, blinking: false, playNotificationSound: false);
                    }

                    Widget2.Stealth = false;
                }

                if (componentInput.m_playerInput.Aim.HasValue)
                {
                    Camera activeCamera = componentInput.m_componentPlayer.GameWidget.ActiveCamera;
                    Vector3 viewPosition = activeCamera.ViewPosition;
                    Vector3 viewDirection = activeCamera.ViewDirection;
                    componentInput.m_playerInput.Aim = new Ray3(viewPosition, viewDirection);
                }

                Subtexture normalSubtexture = ((componentInput.m_componentPlayer.ComponentBody.TargetCrouchFactor == 1f) ? b : a);
                Widget2.NormalSubtexture = normalSubtexture;
            }
            catch
            {
            }
        }

        public override void OnLevelUpdate(ComponentLevel level)
        {
        }

        public override void OnXdbLoad(XElement xElement)
        {
            base.OnXdbLoad(xElement);
            if (ModsManager.FindElementByGuid(xElement, "a86d419e-fbd2-4688-9046-a9d08d3f4316", out var elementout))
            {
                XmlUtils.SetAttributeValue(elementout, "Value", "Game.NewComponentLocomotion");
            }
        }
    }
}